<?php
Prado::using('Application.common.common');
class OrderForm extends TPage
{
	public function onLoad($param)
	{
		parent::onLoad($param);
		$this->Master->pageHead->Title = $this->Application->Parameters["WEBSITE_TITLE"] . " | Order Details";
		if (!$this->IsPostBack)
		{
			$orderStatuses = TPropertyValue::ensureArray($this->Application->Parameters["ORDER_STATUS"]);
			$this->ddlOrderStatus->Items->clear();
			foreach($orderStatuses as $id=>$value)
			{
				$item = new TListItem; 
				$item->Text = $value;
				$item->Value = $id;
				$this->ddlOrderStatus->Items->add($item);
			}
			
			$paymentStatuses = TPropertyValue::ensureArray($this->Application->Parameters["PAYMENT_STATUS"]);
			$this->ddlPaymentStatus->Items->clear();
			foreach($paymentStatuses as $id=>$value)
			{
				$item = new TListItem; 
				$item->Text = $value;
				$item->Value = $id;
				$this->ddlPaymentStatus->Items->add($item);
			}
		
			$this->populateData();
		}
	}
	
	public function populateData()
	{
		$activeRecord = $this->getItem();
		if ($activeRecord instanceof OrderRecord)
		{
			$this->lblHeader->Text = "Order detail: ".$activeRecord->Num;
			$this->lblOrderNum->Text = $activeRecord->Num;
			$this->lblOrderDate->Text = date('d/m/Y h:i:s A',$activeRecord->CreateDate);
			$this->lnkUser->Text = $activeRecord->SFullName;
			//$this->lnkUser->NavigateUrl = $this->Service->ConstructUrl("admin.UserForm",array("id"=>$activeRecord->UserID,"alias"=>$activeRecord->SEmail));

			$this->lblShipping->Text = $activeRecord->SFullName."<br />";
			$this->lblShipping->Text .= $activeRecord->SAddress."<br />";
			$this->lblShipping->Text .= $activeRecord->SCity."<br />";
			$this->lblShipping->Text .= Common::getCountryName($activeRecord->SCountryCode)." ".$activeRecord->SPostalCode."<br />";			
			
			$this->lblContact->Text = "Email: ".$activeRecord->SEmail."<br />";
			$this->lblContact->Text .= "Phone: ".$activeRecord->SPhone;
			
			$sqlmap = $this->Application->Modules['sqlmap']->Client;
			$this->rptOrderItem->DataSource = $sqlmap->queryForList("GetOrderItemByOrderID", $activeRecord->ID);
			$this->rptOrderItem->DataBind();

			$this->nfSubtotal->Value = Common::roundTo($activeRecord->Subtotal);
			$this->memberDiscount->Value = "-" . Common::roundTo($activeRecord->CouponAmount);
			$this->nfShippingAmount->Value = Common::roundTo($activeRecord->ShippingAmount);
			$this->nfTotal->Value = Common::roundTo($activeRecord->TotalAmount);
			$this->txtComments->Text = $activeRecord->Comments;
			$this->txtAdminRemarks->Text = $activeRecord->AdminRemarks;
			$this->txtInternalRemarks->Text = $activeRecord->InternalRemarks;
			
			$this->ddlOrderStatus->SelectedValue = $activeRecord->OrderStatus;
			$this->ddlPaymentStatus->SelectedValue = $activeRecord->PaymentStatus;
			$this->lblPaymentMethod->Text = $activeRecord->PaymentMethod;
			$this->lblShippingMethod->Text = $activeRecord->ShippingMethod;						
		}
	}
	
	protected function getItem()
	{
		if ($this->Request->Contains("id") && $this->Request->Contains("num"))
		{
			$activeRecord = OrderRecord::finder()->findByorder_idAndorder_num(TPropertyValue::ensureInteger($this->Request['id']), $this->Request['num']);
			if($activeRecord === null)
			{
				$this->Notice->Type = AdminNoticeType::Error;
				$this->Notice->Text = $this->Application->getModule("message")->translate("ITEM_NOT_FOUND","order");
				$this->mainBox->Visible = false;
			}
			return $activeRecord;
		}
		else
		{
			$this->Notice->Type = AdminNoticeType::Error;
			$this->Notice->Text = $this->Application->getModule("message")->translate("ITEM_NOT_FOUND","order");
			$this->mainBox->Visible = false;
		}
	}
	
	protected function btnGenerateInvoice_Clicked($sender, $param)
	{
		if ($this->IsValid)
		{
			$activeRecord = $this->getItem();
			$activeRecord->OrderStatus = $this->ddlOrderStatus->SelectedValue;
			$activeRecord->PaymentStatus = $this->ddlPaymentStatus->SelectedValue;
			$activeRecord->Comments = $this->txtComments->Text;
			$activeRecord->AdminRemarks = $this->txtAdminRemarks->Text;
			$activeRecord->InternalRemarks = $this->txtInternalRemarks->Text;
			try
			{
				$activeRecord->save();
				$this->Response->redirect($this->Service->ConstructUrl("admin.OrderInvoice",array("id"=>$activeRecord->ID,"num"=>$activeRecord->Num)));
			}
			catch(TException $ex)
			{
				$this->Notice->Type = AdminNoticeType::Error;
				$this->Notice->Text = $this->Application->getModule("message")->translate("UPDATE_FAILED","order", "");
			}
		}
	}
	
	protected function btnEdit_Clicked($sender, $param)
	{
		if ($this->IsValid)
		{
			$activeRecord = $this->getItem();
			$activeRecord->OrderStatus = $this->ddlOrderStatus->SelectedValue;
			$activeRecord->PaymentStatus = $this->ddlPaymentStatus->SelectedValue;
			$activeRecord->Comments = $this->txtComments->Text;
			$activeRecord->AdminRemarks = $this->txtAdminRemarks->Text;
			$activeRecord->InternalRemarks = $this->txtInternalRemarks->Text;
			try
			{
				$activeRecord->save();
				$action = ($activeRecord->ID>0 ? "update-success" : "add-success");
				$msg = $this->Application->getModule("message")->translate(($activeRecord->ID>0 ? "UPDATE_SUCCESS" : "ADD_SUCCESS"),"Order",$activeRecord->Num);
				$url = $this->Service->ConstructUrl("admin.ManageOrders",array("action"=>$action, "msg"=>$msg));
				$this->Response->redirect($url);
			}
			catch(TException $ex)
			{
				$this->Notice->Type = AdminNoticeType::Error;
				$this->Notice->Text = $this->Application->getModule("message")->translate("UPDATE_FAILED","order", "");
			}
		}
	}
}

?>